angular.module('app').controller('vmAddVmwareCtrl', ['$rootScope', '$scope', '$state', 'httpLoad', '$stateParams', '$modal', function ($rootScope, $scope, $state, httpLoad, $stateParams, $modal) {
	$rootScope.link = '/statics/css/user.css';
	$rootScope.moduleTitle = '平台管理 > VCenter';
	$scope.param = {
		rows: 10
	};
	$scope.showDetail = 1;
	$scope.showVmware = 1;
	$scope.goBack = function () {
		$state.go('app.config.vmwareTab');
		sessionStorage.setItem('prePage', 'true');
	};
	$scope.selectNetworkListData = [];
	(function () {
		$scope.addData = {};
		$scope.param = {};
		$scope.isShowTemplate = true;
		//获取数据存储数据
		$scope.getDatastore = function (vhostId) {
			var params = {
					simple: true
				},
				searchParam = [{"param": {"vendorId": $stateParams.id, "vhostId": vhostId, category: 'PM'}, "sign": "EQ"}];
			params.params = JSON.stringify(searchParam);
			httpLoad.loadData({
				url: '/datastore/list',
				method: 'POST',
				data: params,
				//data: {"id":$stateParams.id,"vdcId":vdcId,"clusterId":clusterId,"vhostId":vhostId},
				noParam: true,
				success: function (data) {
					if (data.success) {
						$scope.datastoreData = data.data.rows;
					}
				}
			});
		};
		//获取目标主机数据
		$scope.getVhost = function (id, from) {
			var searchParam = [];
			if (from == 'vdc') {
				searchParam = [{"param": {"vendorId": $stateParams.id, "vdcId": id}, "sign": "EQ"}]
			} else if (from == 'cluster') {
				searchParam = [{"param": {"vendorId": $stateParams.id, "clusterId": id}, "sign": "EQ"}]
			}
			var params = {
				page: 1,
				rows: 10000
			};
			params.params = JSON.stringify(searchParam);
			httpLoad.loadData({
				url: '/vhost/list',
				method: 'POST',
				data: params,
				//data: {"id":$stateParams.id,"vdcId":vdcId,"clusterId":clusterId},
				noParam: true,
				success: function (data) {
					if (data.success) {
						$scope.vhostData = data.data.rows;
					}
				}
			});
		};
		//获取集群数据
		$scope.getCluster = function (vdcId) {
			var params = {
					simple: true
				},
				searchParam = [{"param": {"vendorId": $stateParams.id, "vdcId": vdcId}, "sign": "EQ"}];
			params.params = JSON.stringify(searchParam);
			httpLoad.loadData({
				url: '/cluster/list',
				method: 'POST',
				data: params,
				//data: {"id":$stateParams.id,"vdcId":vdcId},
				noParam: true,
				success: function (data) {
					if (data.success) {
						$scope.clusterData = data.data.rows;
					}
				}
			});
		};
		//获取数据中心数据
		$scope.getVdc = function () {
			var params = {
					simple: true
				},
				searchParam = [{"param": {"vendorId": $stateParams.id}, "sign": "EQ"}];
			params.params = JSON.stringify(searchParam);
			httpLoad.loadData({
				url: '/vdc/list',
				method: 'POST',
				data: params,
				//data: {"id":$stateParams.id},
				noParam: true,
				success: function (data) {
					if (data.success) {
						$scope.vdcData = data.data.rows;
					}
				}
			});
		};
		if ($scope.showVmware == 1) $scope.getVdc();
		//选择数据中心
		$scope.chooseVdc = function (item) {
			$scope.vdcData.forEach(function (data) {
				data.isVdcActive = false;
				if (data.id == item.id) {
					data.isVdcActive = !item.isVdcActive;
				}
				if (data.isVdcActive) {
					$scope.vdcId = data.id;
					$scope.clusterId = '';
					$scope.addData.hostname = '';
					$scope.addData.datastore = '';
					$scope.datastoreData = '';
					$scope.getCluster($scope.vdcId);
					$scope.getVhost($scope.vdcId, 'vdc');
					return;
				}
			});
		};
		//选择集群
		$scope.chooseCluster = function (item) {
			$scope.clusterData.forEach(function (data) {
				data.isClusterActive = false;
				if (data.id == item.id) {
					data.isClusterActive = !item.isClusterActive;
				}
				if (data.isClusterActive) {
					$scope.clusterId = data.id;
					$scope.addData.hostname = '';
					$scope.addData.datastore = '';
					$scope.datastoreData = '';
					$scope.getVhost($scope.clusterId, 'cluster');
					return;
				}
			});
		};
		//选择目标主机
		$scope.chooseVhost = function (item) {
			if (!item.connected || item.maintain) return $scope.pop('无法选择该虚拟机，因为该虚拟机未连接、处于维护模式下，或者在更改其维护状态', 'info')
			$scope.vhostData.forEach(function (data) {
				data.isVhostActive = false;
				if (data.id == item.id) {
					data.isVhostActive = !item.isVhostActive;
				}
				if (data.isVhostActive) {
					$scope.addData.hostname = data.name;
					$scope.addData.hostId = data.id;
					$scope.addData.datastore = '';
					$scope.datastoreData = '';
					$scope.getDatastore(data.id);
					return;
				}
			});
		};
		//选择数据存储
		$scope.chooseDatastore = function (item) {
			$scope.datastoreData.forEach(function (data) {
				data.isDatastoreActive = false;
				if (data.id == item.id) {
					data.isDatastoreActive = !item.isDatastoreActive;
				}
				if (data.isDatastoreActive) {
					$scope.addData.datastore = data.name;
					$scope.addData.storeId = data.id;
					return;
				}
			});
		};
		//获取模板数据
		$scope.getTemplateData = function () {
			var params = {
					simple: true
				},
				searchParam = [{"param": {"vendorId": $stateParams.id, "isTemplate": true, "status": "STOPPED"}, "sign": "EQ"}];
			params.params = JSON.stringify(searchParam);
			httpLoad.loadData({
				url: '/vm/list',
				method: 'POST',
				data: params,
				noParam: true,
				success: function (data) {
					if (data.success) {
						$scope.templateList = data.data.rows;
					}
				}
			});
		};
		if ($scope.isShowTemplate == true) {
			if ($scope.showVmware == 1) $scope.getTemplateData();
		}
		//检验CPU，内存，磁盘的大小
		$scope.check = function (action, value, min, max) {
			var number = parseInt(value);
			if (action == 0) var name = 'CPU' + '范围是' + min + '-' + max + ',' + '请重新输入';
			if (action == 1) var name = '内存' + '范围是' + min + '-' + max + ',' + '请重新输入';
			if (action == 2) var name = '磁盘' + '范围是' + min + '-' + max + ',' + '请重新输入';
			if (value < min || value > max) {
				$scope.pop(name, 'error');
				return;
			}
		};
		$scope.operationStatus = {
			REMOVE: '删除中',
			ADD: '新增中',
			EDIT: '编辑中'
		};
		$scope.getNetworkData = function () {
			$scope.selectNetworkListData = [];
			if (!$scope.addData.templateId) return;
			httpLoad.loadData({
				url: '/vm/detail',
				method: 'GET',
				data: {
					id: $scope.addData.templateId
				},
				success: function (data) {
					if (data.success) {
						$scope.selectNetworkListData = data.data.netcards;
						$scope.selectNetworkListData.forEach(function (item) {
							item.operation = 'NORMAL';
							item.cacheOperation = 'NORMAL';
							item.disLabel = item.label.replace(/Network adapter/g, '网络适配器');
						})
					}
				}
			});
		};
		$scope.recoveryNetwork = function (row) {
			row.operation = row.cacheOperation;
		};
		$scope.delNetwork = function (row, index) {
			if (!row.cacheOperation) {
				$scope.selectNetworkListData.splice(index, 1);
			} else {
				row.cacheOperation = row.operation;
				row.operation = 'REMOVE';
			}
		};
		$scope.operateNetwork = function (row, index) {
			if (!$scope.addData.hostId) return $scope.pop('请选择目标主机', 'info');
			if (!$scope.addData.templateId) return $scope.pop('请选择虚机模板', 'info');
			var modalInstance = $modal.open({
				templateUrl: '/statics/tpl/config/vmware/vm/addNetworkModal.html',
				controller: 'operateNetworkModalCtrl',
				backdrop: 'static',
				resolve: {
					id: function () {
						return $scope.addData.hostId;
					},
					itemData: function () {
						return row
					}
				}
			});
			modalInstance.result.then(function (data) {
				if (row) {
					$scope.selectNetworkListData[index] = data;
				} else {
					$scope.selectNetworkListData.push(data);
				}
			});
		};
		var formatNetworks = function () {
			var toObjFormat = function (obj) {
				for (var a in obj) {
					if (obj[a] === "") delete obj[a];
				}
				return obj;
			}
			var arr = [];
			$scope.selectNetworkListData.forEach(function (item) {
				if (item.operation != 'NORMAL') {
					var obj = toObjFormat({
						name: item.name,
						type: item.type,
						label: item.label,
						operation: item.operation
					});
					arr.push(obj);
				}
			});
			return arr;
		}
		$scope.ok = function () {
			//云供应商的ID
			$scope.addData.id = $stateParams.id;
			if (!$scope.addData.templateId) {
				$scope.pop('请选择虚机模板', 'error');
				return;
			} else {
				$scope.templateList.forEach(function (data) {
					if (data.id == $scope.addData.templateId) {
						$scope.addData.template = data.name;
					}
				});
			}
			if (!$scope.addData.hostname && $scope.addData.hostname == '') {
				$scope.pop('请选择目标主机', 'error');
				return;
			}
			if (!$scope.addData.datastore && $scope.addData.datastore == '') {
				$scope.pop('请选择数据存储', 'error');
				return;
			}
			$scope.addData.operations = formatNetworks();
			httpLoad.loadData({
				url: '/vm/create',
				method: 'POST',
				data: $scope.addData,
				success: function (data) {
					if (data.success) {
						$scope.pop(data.message);
						$scope.goBack();
					}
				}
			});
		};
		$scope.cancel = function () {
			$scope.goBack();
		};
	})();
}]);